import 'package:flutter/material.dart';
import 'lc_typedef.dart';

/// LCTableViewSessionHeader
class LCTableViewSessionHeader extends StatefulWidget {
  final int sessionIndex;
  final double sessionHeaderHeight;

  final IndexedWidgetBuilder viewForHeaderInSection;
  final DidClickedSessionAtIndex didClickedSessionAtIndex;

  LCTableViewSessionHeader({
    this.sessionIndex,
    this.sessionHeaderHeight,
    this.viewForHeaderInSection,
    this.didClickedSessionAtIndex,
  });

  @override
  _LCTableViewSessionHeaderState createState() =>
      _LCTableViewSessionHeaderState();
}

class _LCTableViewSessionHeaderState extends State<LCTableViewSessionHeader> {
  Offset _downOffset;

  @override
  Widget build(BuildContext context) {
    Widget child = widget.viewForHeaderInSection != null
        ? widget.viewForHeaderInSection(context, widget.sessionIndex)
        : Text('title');

    return Listener(
      behavior: HitTestBehavior.translucent,
      child: Container(
          alignment: Alignment.center,
          color: Color.fromRGBO(221, 221, 221, 1),
          height: widget.sessionHeaderHeight,
          child: child),
      onPointerDown: (event) {
        _downOffset = event.position;
      },
      onPointerUp: (event) {
        if ((event.position.dy - _downOffset.dy).abs() < 5) {
          widget.didClickedSessionAtIndex(widget.sessionIndex, child);
        }
      },
    );
  }
}

/// LCTableViewCell
class LCTableViewCellItem extends StatefulWidget {
  final int sessionIndex;
  final NumberOfRowsInSection numberOfRowsInSection;
  final IndexPathWidgetBuilder cellForRowAtIndexPath;
  final DidClickedCellAtIndexPath didClickedCellAtIndexPath;

  LCTableViewCellItem({
    this.sessionIndex,
    this.numberOfRowsInSection,
    this.cellForRowAtIndexPath,
    this.didClickedCellAtIndexPath,
  });

  @override
  _LCTableViewCellItemState createState() => _LCTableViewCellItemState();
}

class _LCTableViewCellItemState extends State<LCTableViewCellItem> {
  Offset _downOffset;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.separated(
        itemCount: widget.numberOfRowsInSection(widget.sessionIndex),
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemBuilder: (ctx, index) {
          Widget child = widget.cellForRowAtIndexPath != null
              ? widget.cellForRowAtIndexPath(
                  context, IndexPath(session: widget.sessionIndex, row: index))
              : getCell(index);
          return Listener(
            onPointerDown: (event) {
              _downOffset = event.position;
            },
            onPointerUp: (event) {
              if (widget.didClickedCellAtIndexPath != null) {
                if ((event.position.dy - _downOffset.dy).abs() < 5) {
                  print('onPointerUp .. ');
                  widget.didClickedCellAtIndexPath(
                      IndexPath(session: widget.sessionIndex, row: index),
                      child ?? getCell(index));
                }
              }
            },
            child: child,
          );
        },
        separatorBuilder: (ctx, index) {
          return Divider(
            color: Color.fromARGB(255, 221, 221, 221),
            height: 1,
          );
        },
      ),
    );
  }

  Widget getCell(int index) {
    return ListTile(
      title: Text('cell'),
      subtitle: Text('description'),
    );
  }
}
